﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OUnsuitableProdList :PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除, Permission.导出 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OUnsuitableProdList";
            }
        }
        #region 属性 (查询条件)

        /// <summary>
        /// 排序字段 值为 id
        /// </summary>
        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderField", "StampNo");
            }
        }
        /// <summary>
        ///  排序方式 desc或 asc
        /// </summary>
        protected override string orderDirection
        {
            get
            {
                
                return GetRequestValue<string>("orderDirection", "DESC");
            }
        }
        protected override string SQLSort
        {
            get
            {
                return (orderField + " " + orderDirection);
            }
        }

        protected string StampNo
        {
            get
            {
                return GetRequestValue<string>("StampNo", "");

            }
        }
        protected string ItemCode
        {
            get
            {
                return GetRequestValue<string>("ItemCode", "");

            }
        }
       
        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }

        protected string SupplerName
        {
            get
            {
                return GetRequestValue<string>("SupplerName", "");
            }
        }
        protected string DownlineDateFrom
        {
            get
            {
                return GetRequestValue<string>("DownlineDateFrom", "");

            }
        }
        protected string DownlineDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(DownlineDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("DownlineDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(DownlineDateFrom).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
                    else
                        return todate;
                }

            }
        }

        protected string ExportFileName
        {
            get
            {
                return GetRequestValue<string>("ExportFileName", "");
            }
        }
       
        #endregion
        protected string GetWhereSql()
        {
            string wheresql = " 1=1 ";
            if (!string.IsNullOrEmpty(ItemCode))
            {
                wheresql = string.Format(" (itemcode like '%{0}%' or itemname like '%{0}%') ", ItemCode);
            }

            if (!string.IsNullOrEmpty(SerialNumber))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and SerialNumber like '%{0}%'", SerialNumber);
                else
                    wheresql = string.Format(" SerialNumber like '%{0}%'", SerialNumber);
            }

            if (!string.IsNullOrEmpty(StampNo))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and StampNo like'%{0}%'", StampNo);
                else
                    wheresql = string.Format(" StampNo l ike'%{0}%'", StampNo);
            }

            if (!string.IsNullOrEmpty(SupplerName))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and SupplerName like '%{0}%'", SupplerName);
                else
                    wheresql = string.Format(" SupplerName like '%{0}%'", SupplerName);
            }

            string DOWNSQL = "";
            if (!string.IsNullOrEmpty(DownlineDateFrom))
            {
                DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("IssueDate", QueryOperatorsEnum.范围, DownlineDateFrom + "|" + DownlineDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            if (!string.IsNullOrEmpty(ExportFileName))
            {
                if (!string.IsNullOrEmpty(wheresql))
                    wheresql += string.Format(" and ExportFileName like'%{0}%'", ExportFileName);
                else
                    wheresql = string.Format(" ExportFileName like'%{0}%'", ExportFileName);
            }


            return wheresql;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            this.AddLog(Permission.查看, GetWhereSql());
        }

        protected void DataBind()
        {
            this.rpMenu.DataSource = OUnsuitableProdController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
            this.rpMenu.DataBind();
        }

        protected string GetIKCaseStatus(object UnsuitabledID, object SupplerName,object IKCaseStatus,object LoseCtrDpt)
        {
            string temphtml = "<a class='btnDiffAnalyse' href='MacModel/OUnsuitableProdUpdateIKCase.aspx?UnsuitabledID={0}' target='dialog' title='IK件结案相关' rel='OUnsuitableProdUpdateIKCase' max='false' mask='true' maxable='false' minable='false' drawable='true' width='830' height='350'>{1}</a>";
            if (SupplerName.ToString().Trim().ToUpper() == "IK" && LoseCtrDpt.ToString().ToUpper() == "IK")
                return string.Format(temphtml, UnsuitabledID, IKCaseStatus);
            else
                return "";
        }

        protected string GetIKReturn(object UnsuitabledID, object SupplerName)
        {
            string temphtml = "<a class='btnEditMoney' href='MacModel/OUnsuitableProdUpdateIKReturn.aspx?UnsuitabledID={0}' target='dialog' title='IK件相关费用' rel='OUnsuitableProdUpdateIKReturn' max='false' mask='true' maxable='false' minable='false' drawable='true' width='830' height='350'>修改</a>";
            if (SupplerName.ToString().Trim().ToUpper() == "IK")
                return string.Format(temphtml, UnsuitabledID);
            else
                return "";
        }
        
    }
}